package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAccount;
import com.zb.entity.ZjWithdrawSet;
import com.zb.mapper.ZjWithdrawSetMapper;
import com.zb.service.ZjAccountService;
import com.zb.service.ZjWithdrawCheckEmpService;
import com.zb.service.ZjWithdrawSetService;
import com.zb.vo.WithdrawSetVo;
import org.json.JSONString;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 提现设置表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-05-25
 */
@Service
public class ZjWithdrawSetServiceImpl extends ServiceImpl<ZjWithdrawSetMapper, ZjWithdrawSet> implements ZjWithdrawSetService {
    @Resource
    private ZjWithdrawCheckEmpService withdrawCheckEmpService;

    // 初始化 或 恢复初始化提现设置
    @Override
    public void initOrRestoreDefaultWithdrawSet(Long custAccId) {
        ZjWithdrawSet set = new ZjWithdrawSet();
        set.setId(custAccId);
        set.setEnableOrderWithdraw(true);
        set.setWithdrawFreezeDay(7);
        set.setMonthWithdrawQty(10);
        set.setEnableWithdrawCheck(true);
        set.setMinWithdrawMoney(new BigDecimal("100.0"));
        this.saveOrUpdate(set);
    }

    // 初始化默认值
    @Override
    public void initDefaultValue(Long custAccId) {
        initOrRestoreDefaultWithdrawSet(custAccId);
    }

    // 保存提现设置
    @Override
    @Transactional
    public void save(Long custAccId, WithdrawSetVo withdrawSetVo) {
        if (withdrawSetVo.getEnableWithdrawCheck()) {
            Assert.notEmpty(withdrawSetVo.getEmpIds(), "您已开启提现审核功能，审核人员不能为空，至少选择一项");
        }
        ZjWithdrawSet withdrawSet = this.getById(custAccId);
        if (null != withdrawSet) {
            withdrawSet.setEnableOrderWithdraw(withdrawSetVo.getEnableOrderWithdraw());
            withdrawSet.setWithdrawFreezeDay(withdrawSetVo.getWithdrawFreezeDay());
            withdrawSet.setMonthWithdrawQty(withdrawSetVo.getMonthWithdrawQty());
            withdrawSet.setEnableWithdrawCheck(withdrawSetVo.getEnableWithdrawCheck());
            withdrawSet.setMinWithdrawMoney(withdrawSetVo.getMinWithdrawMoney());
            withdrawSet.setWithdrawPayEmp(JSONObject.toJSONString(withdrawSetVo.getPayEmpIds()));
            this.updateById(withdrawSet);
        } else {
            withdrawSet = new ZjWithdrawSet();
            BeanUtil.copyProperties(withdrawSetVo, withdrawSet);
            withdrawSet.setId(custAccId);
            this.save(withdrawSet);
        }
        withdrawCheckEmpService.save(custAccId, withdrawSetVo.getEmpIds());
    }

    @Resource
    private ZjAccountService zjAccountService;
    // 获取提现设置
    @Override
    public WithdrawSetVo getWithdrawSet(Long custAccId) {
        ZjWithdrawSet withdrawSet = this.getById(custAccId);
        WithdrawSetVo vo = new WithdrawSetVo();
        if (null == withdrawSet) {
            vo.setId(custAccId);
            vo.setEnableOrderWithdraw(true);
            vo.setWithdrawFreezeDay(7);
            vo.setMonthWithdrawQty(10);
            vo.setEnableWithdrawCheck(true);
            vo.setMinWithdrawMoney(new BigDecimal("100.0"));
            vo.setEmpIds(new ArrayList<>());
            return vo;
        }
        BeanUtil.copyProperties(withdrawSet, vo);
        vo.setPayEmp(new ArrayList<>());
        if (null != withdrawSet.getWithdrawPayEmp()){
            JSONArray jsonArray = JSONArray.parseArray(withdrawSet.getWithdrawPayEmp());
            if (jsonArray!=null){
                List<Long> javaList = jsonArray.toJavaList(Long.class);
                vo.setPayEmpIds(javaList);
                for (Long aLong : javaList) {
                    ZjAccount zjAccount = zjAccountService.getById(aLong);
                    if (zjAccount==null)continue;
                    vo.getPayEmp().add(zjAccount);
                }
            }
        }
        vo.setEmpIds(withdrawCheckEmpService.getEmpIdByCustAccId(custAccId));
        return vo;
    }

    // 根据商家ID获取提现冻结期
    @Override
    public Integer getWithdrawFreezeDayByCustAccId(Long custAccId) {
        ZjWithdrawSet withdrawSet = this.getById(custAccId);
        return null == withdrawSet? 7 : withdrawSet.getWithdrawFreezeDay();
    }
}
